<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Monitor_model extends MY_Model
{
	private $_host;
	private $_dbname;
	private $_username;
	private $_password;
	private $_port;

	function Monitor_model()
	{
		parent::MY_Model();
		
		$host = $this->db->hostname;
		$arrHost = explode(':',$host);
		
		$this->_host = trim($arrHost[0]);
		$this->_dbname = $this->db->database;
		$this->_username = $this->db->username;
		$this->_password = $this->db->password;
		$this->_port = $this->db->port;
	}
	
	function campaignBad_getAll($from_date, $to_date, $groupid, $page = 1, $limit = 20)
	{
		$data = array();$data['items']  = array();
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_get_campaigns_bad('$from_date','$to_date',$groupid,$page,$limit);") or die(mysqli_error($iconn));
        while($row = mysqli_fetch_object($result))
        {
			$data['items'][] = $row;
		}
		
		mysqli_next_result($iconn);
		$result = mysqli_store_result($iconn);
		$row = mysqli_fetch_object($result);
		$data['pages'] = (int)$row->totalpage;

		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
	}
	
	function userNoAction_getAll($page = 1, $limit = 20)
	{
		$data = array();$data['items']  = array();
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_get_user_to_care($page,$limit);") or die(mysqli_error($iconn));
        while($row = mysqli_fetch_object($result))
        {
			$data['items'][] = $row;
		}
		
		mysqli_next_result($iconn);
		$result = mysqli_store_result($iconn);
		$row = mysqli_fetch_object($result);
		$data['pages'] = (int)$row->totalpage;

		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
	}
	
	function bannerError_getAll($page = 1, $limit = 20)
	{
		$data = array();$data['items']  = array();
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_get_banner_error($page,$limit);") or die(mysqli_error($iconn));
        while($row = mysqli_fetch_object($result))
        {
			$data['items'][] = $row;
		}
		
		mysqli_next_result($iconn);
		$result = mysqli_store_result($iconn);
		$row = mysqli_fetch_object($result);
		$data['pages'] = (int)$row->totalpage;

		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
	}
	
	function bannerNotRun_getAll($page = 1, $limit = 20)
	{
		$data = array();$data['items']  = array();
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_get_banner_nrunning($page,$limit);") or die(mysqli_error($iconn));
        while($row = mysqli_fetch_object($result))
        {
			$data['items'][] = $row;
		}
		
		mysqli_next_result($iconn);
		$result = mysqli_store_result($iconn);
		$row = mysqli_fetch_object($result);
		$data['pages'] = (int)$row->totalpage;

		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
	}
	
	function campaignCompleted_getAll($page = 1, $limit = 20)
	{
		$data = array();$data['items']  = array();
		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		$result = mysqli_query($iconn, "CALL selfserving_get_list_campaign_completed($page,$limit);") or die(mysqli_error($iconn));
        while($row = mysqli_fetch_object($result))
        {
			$data['items'][] = $row;
		}
		
		mysqli_next_result($iconn);
		$result = mysqli_store_result($iconn);
		$row = mysqli_fetch_object($result);
		$data['pages'] = (int)$row->totalpage;

		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
	}
	
	function get_group_list()
	{
		$data = array();

		$iconn = mysqli_connect($this->_host, $this->_username, $this->_password, $this->_dbname, $this->_port);
		mysqli_multi_query($iconn, "CALL selfserving_get_user_group_listall();") or die(mysqli_error($iconn));
        $result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
        {
			$data[] = $row;
		}

		mysqli_free_result($result);
		mysqli_close($iconn);
		return $data;
	}
	
	function online_monitoring_zone($zoneid, $typegroup=0, $user='', $location=0, $page=0, $limit=20, $typeview=0)
	{
		$ci = & get_instance();
		$db = $ci->load->database('slave1',true);
		$this->__setDb($db);
		return $this->getRowsOnMultiQuery("CALL selfserving_online_monitoring_zone($zoneid, $typegroup, '$user', $location, $page, $limit, $typeview)", 'object');
	}
	
	function online_monitoring_zone_bigdata($zoneid, $page=0, $limit=20)
	{
		$ci = & get_instance();
		$db = $ci->load->database('slave1',true);
		$this->__setDb($db);
		return $this->getRowsOnMultiQuery("CALL selfserving_online_monitoring_zone_bigdata($zoneid, $page, $limit)", 'object');
	}
	
	function online_monitoring_zone_schedule_bigdata($zoneid, $typegroup=0, $location=0,$username,$typeview, $page=1, $limit=20)
	{
		$ci = & get_instance();
		$db = $ci->load->database('slave1',true);
		$this->__setDb($db);
		return $this->getRowsOnMultiQuery("CALL selfserving_online_monitoring_zone_schedule_bigdata($zoneid, $typegroup, $location,'$username','$typeview', $page, $limit)", 'object');
        
    }
	
	function online_monitoring_zone_channel($channelid, $page=0, $limit=20)
	{
		$ci = & get_instance();
		$db = $ci->load->database('slave1',true);
		$this->__setDb($db);
		return $this->getRowsOnMultiQuery("CALL selfserving_online_monitoring_zone_channel($channelid, $page, $limit)", 'object');
	}
	
	function online_monitoring_campaign_inqueue()
	{
		$ci = & get_instance();
		$db = $ci->load->database('slave1',true);
		$this->__setDb($db);
		return $this->getRows("CALL selfserving_online_monitoring_campaign_inqueue()", true, 'object');
	}
	
	function online_monitoring_campaign_inqueue_banners($campaignid, $page=0, $limit=20)
	{
		$ci = & get_instance();
		$db = $ci->load->database('slave1',true);
		$this->__setDb($db);
		return $this->getRowsOnMultiQuery("CALL selfserving_online_monitoring_campaign_inqueue_banners($campaignid, $page, $limit)", 'object');
	}
	
	function online_monitoring_user_budget_expen($date, $user='', $typegroup=-1)
	{
		$ci = & get_instance();
		$db = $ci->load->database('slave1',true);
		$this->__setDb($db);
		return $this->getRows("CALL selfserving_tracking_genjs_users_expensed($typegroup, '$user', '$date')", true, 'object');
	}
	
	function online_monitoring_user_zone_numday($zoneid, $user='')
	{
		$ci = & get_instance();
		$db = $ci->load->database('slave1',true);
		$this->__setDb($db);
		return $this->getRows("CALL selfserving_tracking_genjs_get_users_zone_numday('$user', $zoneid)", true, 'object');
	}
    function online_monitoring_campaign_inqueue_bigdata()
	{
		$ci = & get_instance();
		$db = $ci->load->database('slave1',true);
		$this->__setDb($db);
		return $this->getRows("CALL bigdata_queue_campaign_moniter()", true, 'object');
	}
}